﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Remapping a Joystick to Keyboard or Mouse | AutoHotkey v2</title>
<meta name="description" content="Learn how to make joystick buttons or other joystick 控件 发送 keystrokes or mouse clicks." />
<meta name="keywords" content="keyboard,keys,key,keystrokes,clicks,mouse,buttons,button,joystick,hotkeys,热键,macro">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>Remapping a Joystick to Keyboard or Mouse</h1>

<h2 id="toc">目录</h2>
<ul>
  <li><a href="#imp">Important Notes</a></li>
  <li><a href="#button">Making a Joystick Button 发送 Keystrokes or Mouse Clicks</a>
  <ul>
      <li><a href="#different-approaches">Different Approaches</a></li>
      <li><a href="#auto-repeating-joystick-buttons">Auto-repeating a Keystroke</a></li>
      <li><a href="#context-sensitive-joystick-buttons">Context-sensitive Joystick Buttons</a></li>
      <li><a href="#using-a-joystick-as-a-mouse">Using a Joystick as a Mouse</a></li>
  </ul></li>
  <li><a href="#axis">Making Other Joystick 控件 发送 Keystrokes or Mouse Clicks</a>
  <ul>
      <li><a href="#joystick-axes">Joystick Axes</a></li>
      <li><a href="#joystick-pov-hat">Joystick POV Hat</a></li>
      <li><a href="#auto-repeating-other">Auto-repeating a Keystroke</a></li>
  </ul></li>
  <li><a href="#Remarks">Remarks</a></li>
  <li><a href="#related">Related Topics</a></li>
</ul>

<h2 id="imp">Important Notes</h2>
<ul>
  <li>Although a joystick button or axis can be remapped to become a key or mouse button, it cannot be remapped to some other joystick button or axis. That would be possible only with the help of a joystick emulator such as <a href="https://sourceforge.net/projects/vjoystick/">vJoy</a>.</li>
  <li>AutoHotkey identifies each button on a joystick with a unique number between 1 and 32. To determine these numbers, use the <a href="../scripts/index.htm#JoystickTest">joystick test script</a>.</li>
</ul>

<h2 id="button">Making a Joystick Button 发送 Keystrokes or Mouse Clicks</h2>

<h3 id="different-approaches">Different Approaches</h3>
<p>Below are three approaches, starting at the simplest and ending with the most complex. The most complex method works in the broadest variety of circumstances (such as games that require a key or mouse button to be held down).</p>

<h4>Method #1</h4>
<p>This method sends simple keystrokes and mouse clicks. 例如:</p>
<pre>Joy1::<a href="../commands/Send.htm">发送</a> "{Left}"  <em>; Have button #1 发送 a left-arrow keystroke.</em>
Joy2::<a href="../commands/Click.htm">点击</a>  <em>; Have button #2 发送 a 点击 of left mouse button.</em>
Joy3::发送 "a{Esc}{Space}{Enter}"  <em>; Have button #3 发送 the letter &quot;a&quot; followed by Escape, Space, and Enter.</em>
Joy4::发送 "Sincerely,{Enter}John Smith"  <em>; Have button #4 发送 a two-line signature.</em></pre>
<p>To have a button perform more than one function, put the first function <em>beneath</em> the button name and make the last line a <a href="../commands/Return.htm">返回</a>. 例如:</p>
<pre>Joy5::
{
    运行 "notepad"
    等待窗口 "无标题 - 记事本"
    激活窗口
    发送 "This is the text that will appear in Notepad.{Enter}"
}</pre>
<p>See the <a href="../KeyList.htm">Key List</a> 遍历 the complete list of keys and mouse/joystick buttons.</p>

<h4>Method #2</h4>
<p>This method is necessary in cases where a key or mouse button must be held down 遍历 the entire time that you're holding down a joystick button. The following example makes the joystick's second button become the left-arrow key:</p>
<pre>Joy2::
{
    发送 "{Left down}"  <em>; Hold down the left-arrow key.</em>
    <a href="../commands/KeyWait.htm">等待按键</a> "Joy2"  <em>; Wait 遍历 the user to release the joystick button.</em>
    发送 "{Left up}"  <em>; Release the left-arrow key.</em>
}</pre>

<h4>Method #3</h4>
<p>This method is necessary in cases where you have more than one joystick 热键 of the 类型 described in Method #2, and you sometimes press and release such hotkeys simultaneously. The following example makes the joystick's third button become the left mouse button:</p>
<pre>Joy3::
{
    发送 "{LButton down}"   <em>; Hold down the left mouse button.</em>
    设置定时器 "WaitForButtonUp3", 10
}

WaitForButtonUp3()
{
    如果 <a href="../commands/GetKeyState.htm">获取按键状态</a>(&quot;Joy3&quot;)  <em>; The button is still, down, so keep waiting.</em>
        返回
    <em>; Otherwise, the button has been released.</em>
    发送 "{LButton up}"  <em>; Release the left mouse button.</em>
    设置定时器, 0
}
</pre>

<h3 id="auto-repeating-joystick-buttons">Auto-repeating a Keystroke</h3>
<p>Some programs or games might require a key to be sent repeatedly (as though you are holding it down on the keyboard). The following example achieves this by sending spacebar keystrokes repeatedly while you hold down the joystick's second button:</p>
<pre>Joy2::
{
    发送 "{Space down}"   <em>; Press the spacebar down.</em>
    设置定时器 "WaitForJoy2", <strong>30</strong>  <em>; Reduce the number <strong>30</strong> to 20 or 10 to 发送 keys faster. Increase it to 发送 slower.</em>
}

WaitForJoy2()
{
    如果 not 获取按键状态(&quot;Joy2&quot;)  <em>; The button has been released.</em>
    {
        发送 "{Space up}"  <em>; Release the spacebar.</em>
        设置定时器, 0  <em>; Stop monitoring the button.</em>
        返回
    }
    <em>; Since above didn't &quot;返回&quot;, the button is still being held down.</em>
    发送 "{Space down}"  <em>; 发送 another Spacebar keystroke.</em>
}</pre>

<h3 id="context-sensitive-joystick-buttons">Context-sensitive Joystick Buttons</h3>
<p>The <a href="../commands/_HotIf.htm">#动态判断</a> directive can be used to make selected joystick buttons perform a different action (or none at all) depending on any condition, such as the 类型 of window that is active.</p>

<h3 id="using-a-joystick-as-a-mouse">Using a Joystick as a Mouse</h3>
<p>The <a href="../scripts/index.htm#JoystickMouse">Joystick-To-Mouse script</a> converts a joystick into a mouse by remapping its buttons and axis control.</p>

<h2 id="axis">Making Other Joystick 控件 发送 Keystrokes or Mouse Clicks</h2>
<p>To have a script respond to movement of a joystick's axis or POV hat, use <a href="../commands/SetTimer.htm">设置定时器</a> and <a href="../commands/GetKeyState.htm">获取按键状态</a>.</p>

<h3 id="joystick-axes">Joystick Axes</h3>
<p>The following example makes the joystick's X and Y axes behave like the arrow key cluster on a keyboard (left, right, up, and down):</p>
<pre><a href="../commands/SetTimer.htm">设置定时器</a> "WatchAxis", 5

WatchAxis()
{
    JoyX := <a href="../commands/GetKeyState.htm">获取按键状态</a>("JoyX")  <em>; Get position of X axis.</em>
    JoyY := 获取按键状态("JoyY")  <em>; Get position of Y axis.</em>
    KeyToHoldDownPrev := KeyToHoldDown  <em>; Prev now holds the key that was down before (如果 any).</em>

    如果 JoyX &gt; 70
        KeyToHoldDown := "Right"
    否则 如果 JoyX &lt; 30
        KeyToHoldDown := "Left"
    否则 如果 JoyY &gt; 70
        KeyToHoldDown := "Down"
    否则 如果 JoyY &lt; 30
        KeyToHoldDown := "Up"
    否则
        KeyToHoldDown := ""

    如果 KeyToHoldDown = KeyToHoldDownPrev  <em>; The correct key is already down (or no key is needed).</em>
        返回  <em>; Do nothing.</em>

    <em>; Otherwise, release the previous key and press down the new key:</em>
    设置按键延迟 -1  <em>; Avoid delays between keystrokes.</em>
    如果 KeyToHoldDownPrev   <em>; There is a previous key to release.</em>
        发送 "{" KeyToHoldDownPrev " up}"  <em>; Release it.</em>
    如果 KeyToHoldDown   <em>; There is a key to press down.</em>
        发送 "{" KeyToHoldDown " down}"  <em>; Press it down.</em>
}</pre>

<h3 id="joystick-pov-hat">Joystick POV Hat</h3>
<p>The following example makes the joystick's POV hat behave like the arrow key cluster on a keyboard; that is, the POV hat will 发送 arrow keystrokes (left, right, up, and down):</p>
<pre>设置定时器 "WatchPOV", 5

WatchPOV()
{
    POV := 获取按键状态("JoyPOV")  <em>; Get position of the POV control.</em>
    KeyToHoldDownPrev := KeyToHoldDown  <em>; Prev now holds the key that was down before (如果 any).</em>

    <em>; Some joysticks might have a smooth/continous POV rather than one in fixed increments.
    ; To support them all, use a range:</em>
    如果 POV &lt; 0   <em>; No angle to report</em>
        KeyToHoldDown := ""
    否则 如果 POV &gt; 31500                <em>; 315 to 360 degrees: Forward</em>
        KeyToHoldDown := "Up"
    否则 如果 POV &gt;= 0 and POV &lt;= 4500      <em>; 0 to 45 degrees: Forward</em>
        KeyToHoldDown := "Up"
    否则 如果 POV &gt;= 4501 and POV &lt;= 13500  <em>; 45 to 135 degrees: Right</em>
        KeyToHoldDown := "Right"
    否则 如果 POV &gt;= 13501 and POV &lt;= 22500 <em>; 135 to 225 degrees: Down</em>
        KeyToHoldDown := "Down"
    否则                                  <em>; 225 to 315 degrees: Left</em>
        KeyToHoldDown := "Left"

    如果 KeyToHoldDown = KeyToHoldDownPrev  <em>; The correct key is already down (or no key is needed).</em>
        返回  <em>; Do nothing.</em>

    <em>; Otherwise, release the previous key and press down the new key:</em>
    设置按键延迟 -1  <em>; Avoid delays between keystrokes.</em>
    如果 KeyToHoldDownPrev   <em>; There is a previous key to release.</em>
        发送 "{" KeyToHoldDownPrev " up}"  <em>; Release it.</em>
    如果 KeyToHoldDown   <em>; There is a key to press down.</em>
        发送 "{" KeyToHoldDown " down}"  <em>; Press it down.</em>
}</pre>

<h3 id="auto-repeating-other">Auto-repeating a Keystroke</h3>
<p>Both examples above can be modified to 发送 the key repeatedly rather than merely holding it down (that is, they can mimic physically holding down a key on the keyboard). To do this, replace the following line:</p>
<pre>返回  <em>; Do nothing.</em></pre>
<p>With the following:</p>
<pre>{
    如果 KeyToHoldDown
        发送 "{" KeyToHoldDown " down}"  <em>; Auto-repeat the keystroke.</em>
    返回
}</pre>
<h2 id="Remarks">Remarks</h2>
<p>A joystick other than first may be used by preceding the button or axis name with the number of the joystick. 例如, <code>2Joy1</code> would be the second joystick's first button.</p>
<p>To find other useful joystick scripts, visit the <a href="https://www.autohotkey.com/forum/">AutoHotkey forum</a>. A keyword search such as <em>Joystick and 获取按键状态 and 发送</em> is likely to produce topics of interest.</p>
<h2 id="related">Related Topics</h2>
<ul>
    <li><a href="../scripts/index.htm#JoystickMouse">Joystick-To-Mouse script (using a joystick as a mouse)</a></li>
    <li><a href="../KeyList.htm#Joystick">List of joystick buttons, axes, and controls</a></li>
    <li><a href="../commands/GetKeyState.htm">获取按键状态</a></li>
    <li><a href="Remap.htm">Remapping the keyboard and mouse</a></li>
</ul>

</body>
</html>